package com.zy.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

/**
 * 获取周
 * @author 
 *
 */
public class DateUtil {
	/**
	 * 返回起始时间
	 */
	public final static String start ="start";
	/**
	 * 返回结束时间
	 */
	public final static String end ="end";
	
	/**
	 * 获取当前周
	 * @param date
	 * @return
	 */
	 public static int getCurrentWeek(Date date) {
		 Calendar c=Calendar.getInstance();
		 c.setFirstDayOfWeek(Calendar.MONDAY);
		 c.setTime (date);
		 return c.get(Calendar.WEEK_OF_YEAR);
	}
	 
//	 /**
//	  * 获取前一周
//	  * @param date
//	  * @return
//	  */
//	 public static int getPreviousWeek(Date date) {
//		 Calendar c=Calendar.getInstance();
//		 c.setFirstDayOfWeek(Calendar.MONDAY);
//		 c.setTime (date);
//		 return c.get(Calendar.WEEK_OF_YEAR)-1;
//	 }
	 /**
	  * 根据传入的时间，获取年份
	  */
	public static int getCurrentYear(Date date){
		 Calendar cal = Calendar.getInstance();
		 cal.setTime(date);
		 return cal.get(Calendar.YEAR);
	 }
	
	/**
	 * 格式化时期
	 */
	public static String formatDate(Date date){
		SimpleDateFormat da=new SimpleDateFormat("yyyy/MM/dd");
		return da.format(date);
	 }
	
	/**
	 * 获取一周前的起始时间和结束时间
	 * @return  Map 
	 * 			startday 	起始时间
	 * 			endday 		结束时间
	 */
	public static Map<String,Date> getLastWeekMondaySunDay(){ 
		Map<String,Date> map = new HashMap<String,Date>();
		Calendar currentDate = new GregorianCalendar();   
		currentDate.setFirstDayOfWeek(Calendar.MONDAY);  
		
		currentDate.set(Calendar.HOUR_OF_DAY, 0);  
		currentDate.set(Calendar.MINUTE, 0);  
		currentDate.set(Calendar.SECOND, 0);  
		currentDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		currentDate.add(Calendar.DATE, -7);
		map.put("start", currentDate.getTime());
		currentDate.setFirstDayOfWeek(Calendar.MONDAY);  
		currentDate.set(Calendar.HOUR_OF_DAY, 23);  
		currentDate.set(Calendar.MINUTE, 59);  
		currentDate.set(Calendar.SECOND, 59);  
		currentDate.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		map.put("end", currentDate.getTime());
		return map;
	}
	
	/**
	 * 获取两周前的起始时间和结束时间
	 * @return  Map 
	 * 			startday 	起始时间
	 * 			endday 		结束时间
	 */
	public static Map<String,Date> getLastLastWeekMondaySunDay(){
		Map<String,Date> map = new HashMap<String,Date>();
		//SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar currentDate = new GregorianCalendar();   
		currentDate.setFirstDayOfWeek(Calendar.MONDAY);  
		
		currentDate.set(Calendar.HOUR_OF_DAY, 0);  
		currentDate.set(Calendar.MINUTE, 0);  
		currentDate.set(Calendar.SECOND, 0);  
		currentDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		currentDate.add(Calendar.DATE, -14);
		map.put("start", currentDate.getTime());
		currentDate.setFirstDayOfWeek(Calendar.MONDAY);  
		currentDate.set(Calendar.HOUR_OF_DAY, 23);  
		currentDate.set(Calendar.MINUTE, 59);  
		currentDate.set(Calendar.SECOND, 59);  
		currentDate.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		map.put("end", currentDate.getTime());
		return map;
	}
	
	/**
	 * 获取当前周的起始时间和结束时间
	 * @return  Map 
	 * 			startday 	起始时间
	 * 			endday 		结束时间
	 */
	public static Map<String,Date> getCurrentWeekMondaySunDay(){ 
		Map<String,Date> map = new HashMap<String,Date>();
		Calendar currentDate = new GregorianCalendar();   
		currentDate.setFirstDayOfWeek(Calendar.MONDAY);  
		
		currentDate.set(Calendar.HOUR_OF_DAY, 0);  
		currentDate.set(Calendar.MINUTE, 0);  
		currentDate.set(Calendar.SECOND, 0);  
		currentDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		map.put("start", currentDate.getTime());
		currentDate.setFirstDayOfWeek(Calendar.MONDAY);  
		currentDate.set(Calendar.HOUR_OF_DAY, 23);  
		currentDate.set(Calendar.MINUTE, 59);  
		currentDate.set(Calendar.SECOND, 59);  
		currentDate.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		map.put("end", currentDate.getTime());
		return map;
	}
	
	public static void main(String[] args) throws ParseException {
		System.out.println(getCurrentWeekMondaySunDay());
	}
	
	public static String fmtDateToStr(Date date) {
		if (date == null)
			return "";
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			return dateFormat.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
}
